//#include rfm

(function() {

/* Ensure that sessionStorage global object is available */
if (!window.sessionStorage) {
	window.sessionStorage = (function() {
        var _cache = {},
            _keys = [];
        return {
            /**
             * The number of key/value pairs in storage
             */
            length: 0,
            /**
             * Gets the Nth key in storage
             * @param {Number} n The index of the key
             * @returns {String} The Nth key
             */
            key: function(n) {
                return _keys[n];
            },
            /**
             * Gets the data associated with a key
             * @param {String} key The key
             * @returns {String} The data associated with the key
             */
            getItem: function(key) {
                return _cache[key];
            },
            /**
             * Sets the data associated with a key
             * @param {String} key The key under which to store data
             * @param {String} data The data to store
             */
            setItem: function(key, data) {
                if (_keys.indexOf(key) == -1) {
                    _keys.push(key);
                    this.length++;
                }
                _cache[key] = data.toString();
            },
            /**
             * Removes a key/value pair from storage
             * @param {String} key The key to delete
             */
            removeItem: function(key) {
                var i = _keys.indexOf(key);
                if (i > -1) {
                    _keys.splice(i, 1);
                    this.length--;
                }
                delete _cache[key];
            },
            /**
             * Empties the store of all items
             */
            clear: function() {
                _keys = [];
                _cache = {};
                this.length = 0;
            }
        };
	})();
}

/**
 * Local storage utility
 */
rfm.Storage = {
	/**
	 * Accesses the browser's cookies storage API
	 * @returns {Boolean} Whether or not the browser supports cookies
	 * -or-
	 * @param {String} key The name under which the cookie data is stored
	 * @returns {Object} The data stored under that name (if any)
	 * -or-
	 * @param {String} key The name under which the cookie data is stored
	 * @param {Object} data The value to store under that name (defaulted to expire in 180 days)
	 * returns {Object} The data stored under that name (if any)
	 * -or-
	 * @param {String} key The name under which the cookie data is stored
	 * @param {Object} data The value to store under that name
	 * @param {Number} expires The number of milliseconds after which the cookie should expire. If this number is less than 1, the cookie is immediately deleted
	 * returns {Object} The data stored under that name (if any)
	 * -or-
	 * @param {String} key The name under which the cookie data is stored
	 * @param {Object} data The value to store under that name
	 * @param {Date} expires The date after which the cookie should expire. If this date is in the past, the cookie is immediately deleted
	 * returns {Object} The data stored under that name (if any)
	 */
	cookie: function(/*() or (key) or (key, data) or (key, data, expires)*/) {
	    if (document.cookie === undefined) {
	        return;
	    }
	    var key;
	    if (arguments.length < 1) {
	        key = 'supportTest' + (new Date()).valueOf();
	        arguments.callee(key, '1');
		    var supported = arguments.callee(key) == '1';
		    arguments.callee(key, 0, 0);
		    return supported;
	    }
	    else {
	        if (arguments.length < 2) {
		        if (document.cookie !== undefined && document.cookie.length > 0) {
			        var start = document.cookie.indexOf(key + '='),
				        end;
			        if (start > -1) {
				        start = start + key.length + 1;
				        end = document.cookie.indexOf(';', start);
				        if (end < 0) {
				            end = document.cookie.length;
				        }
				        return unescape(document.cookie.substring(start, end));
			        }
		        }
		        return null;
	        }
	        else {
	            var data = arguments[1].toString(),
	                expires = arguments[2];
	            if (expires === undefined) {
			        expires = new Date();
			        expires.setDate(expires.getDate() + 15552000000); // 180 days
	            }
	            else if (typeof expires == 'number' && expires > 0) {
	                expires = new Date(new Date().getTime() + expires);
	            }
	            if (expires < 1) {
	                data = '';
	            }
			    document.cookie = [key, '=', escape(data), ';expires=', expires.toGMTString()].join('');
			    return data;
	        }
	    }
	},
	/**
	 * Accesses the browser's session storage API
	 * @returns {Boolean} Whether or not the browser supports sessionStorage
	 * -or-
	 * @param {String} key The name under which an object is stored
	 * @returns {Object} The data stored under that key (if any)
	 * -or-
	 * @param {String} key The name under which an object is stored
	 * @param {Object} data The object to store under that key
	 * returns {Object} The data stored under that key (if any)
	 */
	session: function(/*() or (key) or (key, data)*/) {
	    if (arguments.length < 1) {
	        return window.Storage && sessionStorage instanceof Storage;
	    }
	    else {
	        var key = arguments[0];
	        if (arguments.length < 2) {
	            return sessionStorage.getItem(key);
	        }
	        else {
	            var data = arguments[1];
	            try {
	                sessionStorage.removeItem(key);
	                sessionStorage.setItem(key, data);
	                return data;
	            }
	            catch (e) {
		            try {
		                sessionStorage.clear();
		                sessionStorage.setItem(key, data);
		                return data;
		            }
		            catch (err) {
		                // Cache is not functioning
		                return data;
		            }
		        }
	        }
	    }
	}
};

})();